﻿using System;
using System.Data.SqlClient;
using System.Drawing;
using System.Web.UI;
using UDS.Components;
using UDS.Entity;

namespace UDS.SubModule.Position
{
    /// <summary>
    /// OPosition 的摘要说明。
    /// </summary>
    public partial class OPositionPage : Page
    {
        private static int DisplayType;
        private static string OffDutyTime = ""; //下班时间
        private static string OnDutyTime = "";
        private static string PositionDescription;
        private static int PositionID; //职位id
        private static string PositionName; //职位名称

        private void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                txtPositionName.Text = "";
                OnDutyTime = "";
                OffDutyTime = "";

                //得到职位ID
                PositionID = (Request.QueryString["PositionID"] == null ? 0 : Int32.Parse(Request.QueryString["PositionID"]));

                var dp = new Components.Position();

                PositionInfo position = UDS.Components.Position.GetPositionInfo(PositionID);
                if (position != null)
                {
                    PositionName = position.Name;
                    PositionDescription = position.Description;
                }

                SqlDataReader dr = dp.GetPositionDutyTime(PositionID);
                if (dr.Read())
                {
                    OnDutyTime = DateTime.Parse(dr["OnDutyTime"].ToString()).ToShortTimeString();
                    OffDutyTime = DateTime.Parse(dr["OffDutyTime"].ToString()).ToShortTimeString();
                }

                if (Request.QueryString["DisplayType"] != null)
                    DisplayType = Int32.Parse(Request.QueryString["DisplayType"]);
                else
                    DisplayType = 0;
                // 显示不同表格
                switch (DisplayType)
                {
                    case 0:
                        tabAdd.Visible = true;
                        tabDelete.Visible = false;
                        tabModify.Visible = false;
                        tabMove.Visible = false;

                        lbAddPosition.BackColor = Color.FromArgb(0xf0f0f0);
                        lbDeletePosition.BackColor = Color.FromArgb(0xffffff);
                        lbModifyPosition.BackColor = Color.FromArgb(0xffffff);
                        lbMovePosition.BackColor = Color.FromArgb(0xffffff);

                        addPositionName.Text = PositionName;
                        break;
                    case 1:
                        tabAdd.Visible = false;
                        tabDelete.Visible = true;
                        tabModify.Visible = false;
                        tabMove.Visible = false;
                        lbAddPosition.BackColor = Color.FromArgb(0xffffff);
                        lbDeletePosition.BackColor = Color.FromArgb(0xf0f0f0);
                        lbModifyPosition.BackColor = Color.FromArgb(0xffffff);
                        lbMovePosition.BackColor = Color.FromArgb(0xffffff);

                        delPositionName.Text = PositionName;
                        break;
                    case 2:
                        tabAdd.Visible = false;
                        tabDelete.Visible = false;
                        tabModify.Visible = true;
                        tabMove.Visible = false;

                        txtPositionName.Text = PositionName;
                        txtMPositionRemark.Text = PositionDescription;

                        lbAddPosition.BackColor = Color.FromArgb(0xffffff);
                        lbDeletePosition.BackColor = Color.FromArgb(0xffffff);
                        lbModifyPosition.BackColor = Color.FromArgb(0xf0f0f0);
                        lbMovePosition.BackColor = Color.FromArgb(0xffffff);

                        txtuOnDutyTime.Text = OnDutyTime;
                        txtuOffDutyTime.Text = OffDutyTime;
                        break;
                    case 3:
                        tabAdd.Visible = false;
                        tabDelete.Visible = false;
                        tabModify.Visible = false;
                        tabMove.Visible = true;

                        lbAddPosition.BackColor = Color.FromArgb(0xffffff);
                        lbDeletePosition.BackColor = Color.FromArgb(0xffffff);
                        lbModifyPosition.BackColor = Color.FromArgb(0xffffff);
                        lbMovePosition.BackColor = Color.FromArgb(0xf0f0f0);

                        lblPosition.Text = PositionName;
                        BindPosition();
                        break;
                    default:
                        break;
                }
            }
        }

        private void BindPosition()
        {
            var db = new Database();
            using (var dr_department = db.RunDataReader("dbo.sproc_GetAllPosition"))
            {
                lstDeparment.DataSource = dr_department;
                lstDeparment.DataTextField = "Position_Name";
                lstDeparment.DataValueField = "Position_ID";
                lstDeparment.DataBind();
            }
        }

        private void cmdAdd_ServerClick(object sender, EventArgs e)
        {
            if (txtAPositionName.Text.Trim() != "")
            {
                long NewDeptID = Components.Position.Add(PositionID, txtAPositionName.Text, txtAPositionRemark.Text);
                if (PositionID > 0)
                {
                    if ((txtOnDutyTime.Text.Trim() != "") && (txtOffDutyTime.Text.Trim() != ""))
                    {
                        if (Components.Position.AddDutyTime(NewDeptID, DateTime.Parse(txtOnDutyTime.Text), DateTime.Parse(txtOffDutyTime.Text)) != 0)
                            Response.Write("<script laguage='javascript'>alert('添加职位出错！');</script>");
                        else
                            Response.Redirect("ListView.aspx?PositionID=" + PositionID + "&Refresh=1");
                    }
                    else
                        Response.Redirect("ListView.aspx?PositionID=" + PositionID + "&Refresh=1");
                }
                else
                    Response.Write("<script laguage='javascript'>alert('添加职位出错！');</script>");
            }
            else
            {
                litAMessage.Text = "请输入职位";
            }
        }

        private void cmdDelete_ServerClick(object sender, EventArgs e)
        {
            switch (Components.Position.Delete(PositionID))
            {
                case 0:
                    Response.Redirect("ListView.aspx?PositionID=" + PositionID + "&Refresh=1");
                    break;
                case -1:
                    Response.Write("<script laguage='javascript'>alert('有下级职位，不能删除！');</script>");
                    break;
                case -2:
                    Response.Write("<script laguage='javascript'>alert('职位有成员，不能删除！');</script>");
                    break;
                default:
                    break;
            }
        }

        private void cmdModify_ServerClick(object sender, EventArgs e)
        {
            if (txtPositionName.Text.Trim() != "")
            {
                if ((Components.Position.Modify(PositionID, txtPositionName.Text, txtMPositionRemark.Text) == 0))
                {
                    if ((txtuOnDutyTime.Text.Trim() != "") && (txtuOffDutyTime.Text.Trim() != ""))
                    {
                        if ((Components.Position.UpdateDutyTime(PositionID, DateTime.Parse(txtuOnDutyTime.Text), DateTime.Parse(txtuOffDutyTime.Text)) != 0))
                        {
                            Response.Write("<script laguage='javascript'>alert('修改职位出错！');</script>");
                            return;
                        }
                    }
                    else
                    {
                        Components.Position.DeleteDutyTime(PositionID);
                    }
                    Response.Redirect("ListView.aspx?PositionID=" + PositionID + "&Refresh=1");
                }
                else
                    Response.Write("<script laguage='javascript'>alert('修改职位出错！');</script>");
            }
            else
                litMMessage.Text = "请输入职位";
        }

        private void lbAddPosition_Click(object sender, EventArgs e)
        {
            Server.Transfer("OPosition.aspx?PositionID=" + PositionID + "&DisplayType=0");
        }

        private void lbDeletePosition_Click(object sender, EventArgs e)
        {
            Server.Transfer("OPosition.aspx?PositionID=" + PositionID + "&DisplayType=1");
        }

        private void lbModifyPosition_Click(object sender, EventArgs e)
        {
            Server.Transfer("OPosition.aspx?PositionID=" + PositionID + "&DisplayType=2");
        }

        private void lbMovePosition_Click(object sender, EventArgs e)
        {
            Server.Transfer("OPosition.aspx?PositionID=" + PositionID + "&DisplayType=3");
        }

        private void cmdMove_Click(object sender, EventArgs e)
        {
            var dept = new Components.Position();

            switch (dept.MoveDeparement(PositionID, Int32.Parse(lstDeparment.Items[lstDeparment.SelectedIndex].Value)))
            {
                case 0:
                    Response.Write("<script laguage='javascript'>alert('移动到" + lstDeparment.Items[lstDeparment.SelectedIndex].Text + " 职位成功！');</script>");
                    Response.Redirect("ListView.aspx?PositionID=" + PositionID + "&Refresh=1");
                    break;
                case -1:
                    Response.Write("<script laguage='javascript'>alert('不能移动到下级职位！');</script>");
                    break;
                default:
                    break;
            }
        }

        private void cmdReturn_Click(object sender, EventArgs e)
        {
            Server.Transfer("Listview.aspx?PositionID=" + PositionID);
        }

        #region Web Form Designer generated code

        protected override void OnInit(EventArgs e)
        {
            //
            // CODEGEN：该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.lbAddPosition.Click += new System.EventHandler(this.lbAddPosition_Click);
            this.lbDeletePosition.Click += new System.EventHandler(this.lbDeletePosition_Click);
            this.lbModifyPosition.Click += new System.EventHandler(this.lbModifyPosition_Click);
            this.lbMovePosition.Click += new System.EventHandler(this.lbMovePosition_Click);
            this.cmdAddReturn.Click += new System.EventHandler(this.cmdReturn_Click);
            this.cmdDeleteReturn.Click += new System.EventHandler(this.cmdReturn_Click);
            this.cmdModifyReturn.Click += new System.EventHandler(this.cmdReturn_Click);
            this.cmdMove.Click += new System.EventHandler(this.cmdMove_Click);
            this.cmdMoveReturn.Click += new System.EventHandler(this.cmdReturn_Click);
            this.cmdAdd.ServerClick += new System.EventHandler(this.cmdAdd_ServerClick);
            this.cmdDelete.ServerClick += new System.EventHandler(this.cmdDelete_ServerClick);
            this.cmdModify.ServerClick += new System.EventHandler(this.cmdModify_ServerClick);
            this.Load += new System.EventHandler(this.Page_Load);
        }

        #endregion
    }
}